<?php

class StudyController extends Application_Model_Controller
{
    /**
     * @var null|Application_Model_StudyMapper
     */
    protected $mapper = null;

    public function init()
    {
        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/study.js');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/css/study.css', 'screen');

        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jqcloud/jqcloud-1.0.0.js');
        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/js/jqcloud/jqcloud.css', 'screen');

        $this->view->headLink()->appendStylesheet($this->view->baseUrl() . '/js/redactor/css/redactor.css', 'screen');
        $this->view->headScript()->appendFile($this->view->baseUrl()  . '/js/redactor/redactor.js');

        $title =  'Учеба';
        $this->view->headTitle($title);
        $this->mapper = new Application_Model_StudyMapper(new Application_Model_DbTable_Terms(), new Application_Model_Term());
        $this->view->termsInAction = $this->mapper->getCollection(array('term_in_action' => 1));
        $this->view->tplSidebar = 'study.phtml';
    }

    public function indexAction()
    {
        $tag = $this->getRequest()->getParam('tag');
        $symbol = $this->getRequest()->getParam('letter');
        $query  = $this->_getParam('q');

        $filters = array(
            'tag_id'       => $tag,
            'first_symbol' => $symbol,
            'query'        => $query,
        );

        $page      = $this->getRequest()->getParam('page', 1);
        $tags      = $this->mapper->fetchAllTags();
        $letters   = $this->mapper->fetchAllFirstSymbols();
        $termsInfo = $this->mapper->getTermsByOptions($filters, $page);
        $this->view->searchText = $this->mapper->getSearchText($filters);
        $this->view->tags      = $tags;
        $this->view->terms     = $termsInfo['terms'];
        $this->view->paginator = $termsInfo['paginator'];
        $this->view->letters   = $letters;
        $this->view->query = $query;
    }
}
